Conditions | 1 |
Paths | 256 |
Total Lines | 149 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
24 | function TreeNode (value) { |
||
25 | var self = this |
||
26 | /** |
||
27 | * @template {V} |
||
28 | * @typedef {Object.<string, TreeNode<V>>} |
||
29 | */ |
||
30 | var children = {} |
||
31 | |||
32 | /** |
||
33 | * Adds new child to current node. |
||
34 | * |
||
35 | * @param {string} name Child name |
||
36 | * @param {TreeNode<V>} child Child value |
||
37 | * @return {TreeNode<V>} Returns added child |
||
38 | */ |
||
39 | this.addChild = function (name, child) { |
||
40 | children[name] = child |
||
41 | return child |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Removes child |
||
46 | * |
||
47 | * @param {string} name Child name |
||
48 | * @return {TreeNode<V>|null} Removed child value |
||
49 | */ |
||
50 | this.removeChild = function (name) { |
||
51 | var value = children[name] |
||
52 | delete children[name] |
||
53 | return value || null |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Fetches current node child by it's name. |
||
58 | * |
||
59 | * @param {string} name Child name. |
||
60 | * |
||
61 | * @return {TreeNode.<V>|null} Existing node or nothing. |
||
62 | */ |
||
63 | this.child = function (name) { return children[name] || null } |
||
64 | |||
65 | /** |
||
66 | * Sets value for current node. |
||
67 | * |
||
68 | * @param {V} v |
||
69 | * @return {TreeNode} Returns current instance. |
||
70 | */ |
||
71 | this.set = function (v) { |
||
72 | value = v |
||
73 | return self |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Returns current node value. |
||
78 | * |
||
79 | * @return {V} |
||
80 | */ |
||
81 | this.get = function () { return typeof value === 'undefined' ? null : value } |
||
82 | |||
83 | /** |
||
84 | * Retrieves node at path |
||
85 | * |
||
86 | * @param {string[]} path |
||
87 | * @return {TreeNode<V>|null} |
||
88 | */ |
||
89 | this.traverse = function (path) { |
||
90 | if (path.length === 0) { |
||
91 | return this |
||
92 | } |
||
93 | var name = path.shift() |
||
94 | var cursor = this |
||
95 | while (name && cursor) { |
||
96 | cursor = cursor.child(name) |
||
97 | name = path.shift() |
||
98 | } |
||
99 | return cursor || null |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Picks branch as a list of nodes (starting from the root) that match |
||
104 | * requested path the most. In the best case, branch would contain root and |
||
105 | * all requested nodes, in the worst - branch would consist of root only. |
||
106 | * |
||
107 | * @param {string[]} path Branch path as list of child names. |
||
108 | * @return {[TreeNode<V>]} Picked branch. |
||
109 | */ |
||
110 | this.branch = function (path) { |
||
111 | var branch = [self] |
||
112 | var cursor = self |
||
113 | var name = path.shift() |
||
114 | while (name) { |
||
115 | cursor = cursor.child(name) |
||
116 | if (!cursor) { |
||
117 | break |
||
118 | } |
||
119 | branch.push(cursor) |
||
120 | name = path.shift() |
||
121 | } |
||
122 | return branch |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Puts provided value at designated path down the tree. |
||
127 | * |
||
128 | * @param {string[]} path Path at which value has to be set. |
||
129 | * @param {V} value Value to be set. |
||
130 | * @return {TreeNode<V>} Created or updated node. |
||
131 | */ |
||
132 | this.put = function (path, value) { |
||
133 | var cursor = self |
||
134 | var name = path.shift() |
||
135 | while (name) { |
||
136 | if (!cursor.child(name)) { |
||
137 | cursor.addChild(name, new TreeNode(null)) |
||
138 | } |
||
139 | cursor = cursor.child(name) |
||
140 | name = path.shift() |
||
141 | } |
||
142 | return cursor.set(value) |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Fetches value closest to provided path. If node contains falsey value, it |
||
147 | * is skipped. |
||
148 | * |
||
149 | * @param {string[]} path Path to pick the branch. |
||
150 | * @return {V|null} Target value or null |
||
151 | */ |
||
152 | this.retrieve = function (path) { |
||
153 | return self.branch(path).reverse().reduce(function (carrier, node) { |
||
154 | return carrier || node.get() |
||
155 | }, null) |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Removes node at path and returns it's value |
||
160 | * @param {string[]} path |
||
161 | * @return {V|undefined} |
||
162 | */ |
||
163 | this.remove = function (path) { |
||
164 | if (path.length === 0) { |
||
165 | throw new Error('You can\'t remove root node') |
||
166 | } |
||
167 | var segment = path.pop() |
||
168 | var node = this.traverse(path) |
||
169 | var child = node ? node.removeChild(segment) : null |
||
170 | return child ? child.get() : null |
||
171 | } |
||
172 | } |
||
173 | |||
321 |